package com.baikeyang.app.controller.admin;

import com.baikeyang.common.bean.PageResponseResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Resource;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.resource.IResourceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by lenovo on 2018/7/17.
 */
@RestController(value = "resourceController")
@RequestMapping(value = "/admin/resource")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @RequestMapping(value = "index")
    public ModelAndView index () {
        return new ModelAndView("/admin/resource/resource-index");
    }


    @RequestMapping(value = "list")
    public Object getSysLogs (String page, String recPerPage, String search, String sortBy, String order) {
        //list?page=0&recPerPage=10&sortBy=&order=asc
        Page<Resource> pageParam = new Page<Resource>();
        Integer size = 10;
        Integer current = 1;
        if(StringUtils.isNotEmpty(recPerPage)){
            try {
                size = Integer.parseInt(recPerPage);
            } catch (Exception e) {}
        }
        if(StringUtils.isNotEmpty(page)) {
            try {
                current = Integer.parseInt(page);
                if(current < 1){current = 1;}
            } catch (Exception e) {}
        }
        pageParam.setSize(size);
        pageParam.setCurrent(current);
        IPage<Resource> pageResult = resourceService.getResourcePage(pageParam);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setResult("success");
        pageResponseResult.setData(pageResult.getRecords());
        pageResponseResult.setPager(current, pageResult.getTotal());
        return pageResponseResult;
    }

    @RequestMapping(value = "add")
    public ModelAndView add () {
        return new ModelAndView("/admin/resource/resource-add");
    }

    @RequestMapping(value = "getResourceParents")
    public ResponseResult getResourceParents() {
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, resourceService.getResourceParentAll());
    }

    @RequestMapping(value = "getAllResources")
    public Object getAllResources() {
        return resourceService.getAllResources();
        //return new ResponseResult(Constants.EXECUTESTATUS.成功, Constants.EXECUTEINFO.成功, Constants.EXECUTECODE.CODE_200, resourceService.getAllResources());
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit (String id) {
        return new ModelAndView("/admin/resource/resource-edit")
                .addObject("res", resourceService.getById(id));
    }

    @RequestMapping(value = "show")
    public ModelAndView show () {
        return new ModelAndView("/admin/resource/resource-show");
    }

    @RequestMapping(value = "save")
    public ResponseResult save (Resource resource) {
        return resourceService.saveResource(resource);
    }

    @RequestMapping(value = "update")
    public ResponseResult update (Resource resource) {
        return resourceService.update(resource);
    }

    @RequestMapping(value = "delete")
    public ResponseResult delete (String ids) {
        return resourceService.delete(ids);
    }

}
